package LeetCode;



import java.util.ArrayList;
import java.util.List;

public class LeetCode230 {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

    List<Integer> list = new ArrayList<>();
    static int count = 0;
  // 思路一：使用中序遍历将有序的值重新放在列表中
    public int kthSmallest(TreeNode root, int k) {
        if (root == null){
            return 0;
        }
        midGet(root,k);
        return list.get(k - 1);
    }

    public void midGet(TreeNode node,int k){
        if (node == null){
            return;
        }
        midGet(node.left,k);
        list.add(node.val);
        count++;
        if (count == k){
            return;
        }
        midGet(node.right,k);
    }
}
